﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmnhanvien : Form
    {
        public frmnhanvien()
        {
            InitializeComponent();

            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            
            col.DataPropertyName = "Ma_NV";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Mã NV";
            dt_BenhNhan.Columns.Add(col);

            TV_Tonghop.Themcot("TEN_NV", "Tên Nhân Viên", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);


            #region Hien
            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "GT";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Giới tính";
            dt_BenhNhan.Columns.Add(col);


            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "NAMSINH_NV";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Năm Sinh Nhân Viên";
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dt_BenhNhan.Columns.Add(col);

            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "TUOI_NV";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Tuổi";
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dt_BenhNhan.Columns.Add(col);

            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "tencd";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Chức Danh";
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dt_BenhNhan.Columns.Add(col);


            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "ten_hv";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Tên Học Vị";
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dt_BenhNhan.Columns.Add(col);

            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "SDT_NV";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Số Điện Thoại";
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dt_BenhNhan.Columns.Add(col);

            col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = "DIACHI_NV";
            col.Name = col.DataPropertyName;
            col.HeaderText = "Địa Chỉ";
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dt_BenhNhan.Columns.Add(col);
            #endregion

            #region An di
            col = new DataGridViewTextBoxColumn();
            col.Visible = false;
            col.DataPropertyName = "GioiTinh_NV";
            col.Name = col.DataPropertyName;
            dt_BenhNhan.Columns.Add(col);

            col = new DataGridViewTextBoxColumn();
            col.Visible = false;
            col.DataPropertyName = "macd";
            col.Name = col.DataPropertyName;
            dt_BenhNhan.Columns.Add(col);

            col = new DataGridViewTextBoxColumn();
            col.Visible = false;
            col.DataPropertyName = "ma_hv";
            col.Name = col.DataPropertyName;
            dt_BenhNhan.Columns.Add(col);
            #endregion




        }

        //ham ket noi co so du lieu
        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }


        private void frmnhanvien_Load(object sender, EventArgs e)
        {
            /////////////////////////;
            tenNV.Text = "Nguyen Thi Thuy";
           // frmtiepdon._abc = tenNV.Text;
            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienThiNhanVien", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dt_BenhNhan.DataSource = dt;// do du lieu vao DataGridView dt_BenhNhan
            // SqlConnection ketnoi1 = getconnect();
            // ketnoi1.Open();
            // SqlDataAdapter ad1 = new SqlDataAdapter("select * from NHAN_VIEN", ketnoi1); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            // DataTable dt1 = new DataTable();
            // ad1.Fill(dt1);
            // dt_NhanVien.DataSource = dt1;// do du lieu vao DataGridView dt_BenhNhan
            ketnoi.Open();
            SqlCommand cmd1 = new SqlCommand("select * from Chuc_danh", ketnoi);
            DataTable tb1 = new DataTable();
            tb1.Load(cmd1.ExecuteReader());

            cboMaCD.DataSource = tb1;
            cboMaCD.DisplayMember = "Tencd";
            cboMaCD.ValueMember = "MaCD";

            SqlCommand cmd2 = new SqlCommand("select * from Hoc_Vi", ketnoi);
            DataTable tb2 = new DataTable();
            tb2.Load(cmd2.ExecuteReader());

            cboMaHV.DataSource = tb2;
            cboMaHV.DisplayMember = "Ten_HV";
            cboMaHV.ValueMember = "Ma_HV";
        }

        private void bt_Them_Click(object sender, EventArgs e)
        {

            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemNhanVien", ketnoi);
            //string sql = @"insert into Nhan_vien(Ma_NV,ten_NV,Chucdanh_nv,Gioitinh_NV,Namsinh_NV,Diachi_NV,Tuoi_NV,SDT_NV,MaCD,Ma_HV) values (maNV.Text, tenNV.Text, cdnhanvien.Text, gioitinh.Text,namsinh.Text,diachi.Text,Convert.ToInt32(tuoi.Text),sodienthoai.Text,maCD.Text,maHV.Text) ";
            //SqlCommand cmd = new SqlCommand(@"insert into Nhan_vien(Ma_NV,ten_NV,Chucdanh_nv,Gioitinh_NV,Namsinh_NV,Diachi_NV,Tuoi_NV,SDT_NV,MaCD,Ma_HV) values (maNV.Text, tenNV.Text, cdnhanvien.Text, gioitinh.Text, namsinh.Text, diachi.Text, Convert.ToInt32(tuoi.Text), sodienthoai.Text, maCD.Text, maHV.Text) ",ketnoi);

            //  cmd.ExecuteNonQuery();


            // SqlCommand cmd = new SqlCommand(sql, ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;      // kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_nv", SqlDbType.Char).Value = maNV.Text;
            cmd.Parameters.Add("@macd", SqlDbType.Char).Value = cboMaCD.SelectedValue;
            cmd.Parameters.Add("@ma_hv", SqlDbType.Char).Value = cboMaHV.SelectedValue;
            cmd.Parameters.Add("@ten_nv", SqlDbType.NVarChar).Value = tenNV.Text;
            cmd.Parameters.Add("@gioitinh_nv", SqlDbType.Bit).Value = radioNam.Checked;
            cmd.Parameters.Add("@namsinh_nv", SqlDbType.Char).Value = namsinh.Text;
            cmd.Parameters.Add("@diachi_nv", SqlDbType.NVarChar).Value = diachi.Text;
            cmd.Parameters.Add("@tuoi_nv", SqlDbType.Int).Value = Convert.ToInt32(tuoi.Text);
            cmd.Parameters.Add("@sdt_nv", SqlDbType.VarChar).Value = sodienthoai.Text;

            // cái nỳ là seo vậy, bang nao ak, bang NHAN_VIEN, cái procedure này ở đâu 

            cmd.ExecuteNonQuery();
            frmnhanvien_Load(sender, e);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatNhanVien", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_nv", SqlDbType.Char).Value = maNV.Text;
            cmd.Parameters.Add("@macd", SqlDbType.Char).Value = cboMaCD.SelectedValue;
            cmd.Parameters.Add("@ma_hv", SqlDbType.Char).Value = cboMaHV.SelectedValue;
            cmd.Parameters.Add("@ten_nv", SqlDbType.NVarChar).Value = tenNV.Text;
            cmd.Parameters.Add("@gioitinh_nv", SqlDbType.Bit).Value = radioNam.Checked;
            cmd.Parameters.Add("@namsinh_nv", SqlDbType.Char).Value = namsinh.Text;
            cmd.Parameters.Add("@diachi_nv", SqlDbType.NVarChar).Value = diachi.Text;
            cmd.Parameters.Add("@tuoi_nv", SqlDbType.Int).Value = Convert.ToInt32(tuoi.Text);
            cmd.Parameters.Add("@sdt_nv", SqlDbType.VarChar).Value = sodienthoai.Text;
            cmd.ExecuteNonQuery();
            frmnhanvien_Load(sender, e);
        }// rôồi ak, tý sửa lại mấy cái procedure mà sdt là int ak, taạ trong csdl là nvarchar, vay sua lai kieu varchar hay theo kieu int ?, sữa cho giống với csdl, csdl là nvarchar(20) thì phải sưa theo, uhm,ok

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaNhanVien", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@ma_nv", SqlDbType.Char).Value = maNV.Text;
            cmd.ExecuteNonQuery();
            frmnhanvien_Load(sender, e);
        }

        int dong;
        private void dt_NhanVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            maNV.Text = dt_BenhNhan.Rows[dong].Cells["Ma_NV"].Value.ToString();
            tenNV.Text = dt_BenhNhan.Rows[dong].Cells["Ten_NV"].Value.ToString();
            radioNam.Checked = (bool)dt_BenhNhan.Rows[dong].Cells["Gioitinh_NV"].Value;
            radioNu.Checked = !radioNam.Checked;
            namsinh.Text = dt_BenhNhan.Rows[dong].Cells["NAMSINH_NV"].Value.ToString();
            tuoi.Text = dt_BenhNhan.Rows[dong].Cells["TUOI_NV"].Value.ToString();
            sodienthoai.Text = dt_BenhNhan.Rows[dong].Cells["SDT_NV"].Value.ToString();
            diachi.Text = dt_BenhNhan.Rows[dong].Cells["DIACHI_NV"].Value.ToString();
        }

     

       

       




    }
}